package com.mysoft.gfyx.marketdevelop.service.impl;

import com.mysoft.framework.common.exception.BusinessLogicException;
import com.mysoft.gfyx.marketdevelop.mip.GetDeveloperDataService;
import com.mysoft.gfyx.marketdevelop.mip.dto.BaseTjListResponseDto;
import com.mysoft.gfyx.marketdevelop.mip.dto.GetDeveloperFilterDataRequestDto;
import com.mysoft.gfyx.marketdevelop.mip.dto.GetDeveloperSortDto;
import com.mysoft.gfyx.marketdevelop.mip.dto.TjMetric.GetMetricListRequestDto;
import com.mysoft.gfyx.marketdevelop.mip.dto.TjMetric.GetMetricListResponseDto;
import com.mysoft.gfyx.marketdevelop.service.DeveloperService;
import com.mysoft.gfyx.marketdevelop.service.MetricService;
import com.mysoft.gfyx.marketdevelop.service.dto.metric.IndicatorDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@Service
public class MetricServiceImp implements MetricService {
    @Autowired
    private DeveloperService developerService;

    @Autowired
    private GetDeveloperDataService getDeveloperDataService;

    public List<IndicatorDto> getMetricList(String metricClassId) {
        GetMetricListRequestDto dto = new GetMetricListRequestDto();
        dto = developerService.setCommonData(dto);
        dto.setId(metricClassId);
        GetMetricListResponseDto metrics = getDeveloperDataService.getMetricList(dto.getProject_code(), dto.getId());

        if (metrics.getData() == null) {
            throw new BusinessLogicException("该分类下没有指标或者指标尚未发布");
        }
        LinkedList res = new LinkedList();
        metrics.getData().forEach(m -> {
            IndicatorDto indicatorDto = new IndicatorDto();
            indicatorDto.setIndicator_id(m.get("id").toString());
            indicatorDto.setIndicator_code(m.get("indicator_code").toString());
            res.add(indicatorDto);
        });

        return res;
    }

    public int getMaxTakingLandNumber() {
        return getMaxValue("nearly_a_year_taken_land");
    }

    public int getMaxOpeningProjectNumber() {
        return getMaxValue("nearly_a_year_open_project");
    }

    public int getMaxCommenceNumber() {
        return getMaxValue("commence_permit_cnt_1year");
    }

    protected int getMaxValue(String field) {
        GetDeveloperFilterDataRequestDto dto = new GetDeveloperFilterDataRequestDto();

        dto = developerService.setCommonData(dto);
        dto.setDetail_table_name(DeveloperService.TABLE_METRICS);
        dto.setPage(1);
        dto.setPage_size(1);

        // 排序
        ArrayList<GetDeveloperSortDto> sorts = new ArrayList<>();
        GetDeveloperSortDto sort = new GetDeveloperSortDto();
        sort.setSort_type(GetDeveloperSortDto.SORT_DESC);
        sort.setAttribute_name(field);
        sort.setTable_name(DeveloperService.TABLE_METRICS);
        sorts.add(sort);
        dto.setSorts(sorts);

        BaseTjListResponseDto res = getDeveloperDataService.getFilteredDeveloperDetailList(dto);
        if (res.getData().getList().size() == 0) {
            return 0;
        } else {
            return Math.round(Float.parseFloat(res.getData().getList().get(0).getItem().get(field).toString()));
        }
    }
}
